//
//  GQHOrderDetailController.m
//  Xanthium
//
//  Created by GuanQinghao on 14/05/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHOrderDetailController.h"
#import "GQHOrderDetailView.h"
#import "GQHOrderModel.h"
#import "GQHHeader.h"
#import "UIButton+WebCache.h"

@interface GQHOrderDetailController () {
    
#pragma mark Data
    
#pragma mark View
    /// 自定义根视图
    GQHOrderDetailView *rootView;
}

@end


@implementation GQHOrderDetailController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHOrderDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_qh_orderID forKey:@"id"];
    [self fetchOrderDetailWithParameters:parameters];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    
    self.qh_titleLabel.text = @"运单详情";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchOrderDetailWithParameters:(NSMutableDictionary *)parameters {
    
    [GQHOrderModel qh_fetchOrderDetailWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, GQHOrderModel *order) {
        
        if (status) {
            
            [rootView.qh_contentArray qh_safetySetObject:order.qh_driverName atIndex:0];
            
            NSString *contentString = [NSString stringWithFormat:@"%@/%.2f吨/%@/%@",order.qh_freightType,order.qh_freightWeight,order.qh_model,order.qh_normalLength];
            [rootView.qh_contentArray qh_safetySetObject:contentString atIndex:1];
            
            [rootView.qh_contentArray qh_safetySetObject:order.qh_contractPrepaymentSum atIndex:2];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_contractFinalPaymentSum atIndex:3];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_contractOtherExpenseSum atIndex:4];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_amount atIndex:5];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_loadingTime atIndex:6];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_departure atIndex:7];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_ownerName atIndex:8];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_driverUnloadedTime atIndex:9];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_destination atIndex:10];
            [rootView.qh_contentArray qh_safetySetObject:order.qh_consignee atIndex:11];
            //回执单照片
            rootView.qh_orderNumberLabel.text = order.qh_orderNumber;
            
            
            NSMutableArray * urlArray = [NSMutableArray array];
            if ([order.qh_attachment containsString:@","])
            {
                NSArray * array = [order.qh_attachment componentsSeparatedByString:@","];
                for (NSString * url in array)
                {
                    [urlArray addObject:[[NSString qh_imageURLString] stringByAppendingPathComponent:url]];
                }
            }
            else
            {
                [urlArray addObject:[[NSString qh_imageURLString] stringByAppendingPathComponent:order.qh_attachment]];
            }
            rootView.imageUrlArray = urlArray;
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
        
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
